package master;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle;
import javax.swing.table.TableCellRenderer;




import common.JobObject;
import common.JobsPackage;
import common.WorkerObject;

public class WorkerGUI extends javax.swing.JFrame{
	private MasterService masterService;
	private JTable jTable1;
	 private JButton jButton1;
	    private JButton jButton2;
	private JScrollPane jScrollPane1;
	
	WorkerGUI(MasterService masterService) {
		this.masterService = masterService;
		this.setTitle("Worker Info Service");
		initComponents();
		
	}
	
	private void initialButton() {

        jButton1 = new JButton();

        jButton1.setText("Refresh");

        
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                refresh();
            }
        });
        //Get Results anytime
        /*// If there exists s Job hasn't finished, then disabled the "Get Result" button.
        for (JobObject job : client.jp.getJobs()) {
            if ("WAITING".equals(job.getStatus()) || "RUNNING".equals(job.getStatus())) {
                jButton2.setEnabled(false);
                break;
            }
        }*/
    }
	
	private void initComponents() {

		jTable1 = new JTable();
        jScrollPane1 = new JScrollPane();
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jScrollPane1.setName("jScrollPane1");
        
        //this.initialLabel();
        this.initialButton();
        this.initialTable();
        //Timer timer = new Timer(this);
        //timer.start();

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(274, 274, 274)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }
	
	
	private void initialTable() {
        jTable1.setName("jTable1");
        jScrollPane1.setViewportView(jTable1);
        Iterator<Integer> iter = masterService.workerInfo.keySet().iterator();
        int numItems = masterService.workerInfo.keySet().size();
        Object[][] objects = new Object[numItems][4];
        int i = 0;
        while (iter.hasNext()) {
	        WorkerObject worker = masterService.workerInfo.get(iter.next());
        	Object[] line = new Object[4];
	            
	        // Assign the value to the line object.
	        line[0] = String.valueOf(worker.getWorkerID());
	        line[1] = String.valueOf(worker.getRegTime());
	        line[2] = String.valueOf(worker.getPrice());
	        line[3] = String.valueOf(worker.getStatus());
	        objects[i++] = line;

	    }

	    jTable1.setModel(new javax.swing.table.DefaultTableModel(objects, new String[] { "WORKER ID", "TIMESTAMP", "PRICE/SEC", "STATE"}));
	}
	
	
    public void refresh() {
    	initialTable();

    }
    
}
